﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SPWebShell.entity
{
    //public class SPListItem
    //{
    //    /// <summary>
    //    /// Sets/gets the ID of the 
    //    /// </summary>
    //    public int SPID { get; set; }
    //    /// <summary>
    //    /// Sets/gets the FileLeafRef without the ID delimiter
    //    /// </summary>
    //    public string Name { get; set; }
    //    Dictionary<string, object> _namevalues;
    //    public SPListItem()
    //    {
    //        _namevalues = new Dictionary<string, object>();
    //    }
    //    public Dictionary<string,object> FieldValues 
    //        {
    //            get
    //            {
    //                return _namevalues;
    //            }
    //        }
    //}
    public class SPListItem
    {
        /// <summary>
        /// Sets/gets the ID of the 
        /// </summary>
        public int SPID { get; set; }
        public string Title { get; set; }
        public DateTime Created { get; set; }
        public DateTime Modified { get; set; }
        public string ModifiedBy { get; set; }
        public string CreatedBy { get; set; }

        /// <summary>
        /// Sets/gets the FileLeafRef without the ID delimiter
        /// </summary>
        public string Name { get; set; }
        Dictionary<string, object> _namevalues;
        public SPListItem()
        {
            _namevalues = new Dictionary<string, object>();
        }
        public Dictionary<string, object> FieldValues
        {
            get
            {
                return _namevalues;
            }
        }
        public SPListItem Clone()
        {
            return (SPListItem)base.MemberwiseClone();
        }
        /// <summary>
        /// Returns the value of the field with the specified key. Tries to convert the case
        /// </summary>
        /// <param name="fieldname"></param>
        /// <returns></returns>
        public object this[string fieldname]
            { 
                get
                {
                    if (string.IsNullOrEmpty(fieldname)) throw new ArgumentNullException("fieldname");
                    fieldname = fieldname.Trim();
                    foreach(string key in _namevalues.Keys)
                    {
                        if (key.Equals(fieldname , StringComparison.CurrentCultureIgnoreCase))
                        {
                            return _namevalues[key];
                        }
                    }
                    throw new KeyNotFoundException(fieldname);
                }
            }
    }
}
